package com.wyz.dao;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.util.List;


public class Dao<T> {
    static QueryRunner qr = null;
    static ComboPooledDataSource ds = null;
    static {
        ds = new ComboPooledDataSource();
        qr = new QueryRunner(ds);
    }
//    对数据库的增删改
    public int update(String sql,Object... params){
        try {
            return qr.update(sql,params);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }
//    单行查询
    public Object SelectMany(String sql, Class<T> clazz, Object... params){
        try {
            return qr.query(sql,new BeanHandler<>(clazz),params);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    public List<T> SelectMore(String sql, Class<T> clazz, Object... params){
        try {
            return qr.query(sql,new BeanListHandler<>(clazz),params);
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }
}
